#include "../include/client.h"

#if 0
//用户输入转换成协议语
extern void usr_analysis(buf,&temp)
{
	char *temp[3];
	temp[0] = buf;
	while(buf)
	{
		if(' ' == *buf)
		{
			*buf = '\0';
			
		}
	}
}
#endif
void usr_analysis(char*buf,prol_t*temp)//用户输入转换成协议语
{	
	char*tmp;
	char*argv1;
	char*argv2;
	char*argv3;
	char*argv4;
	if(buf!=NULL){
		tmp=strtok(buf," ");
		argv1=strtok(NULL," ");
		argv2=strtok(NULL," ");
		argv3=strtok(NULL," ");
		argv4=strtok(NULL," ");
	}
	if(strcmp(tmp,"login")==0)
	{
		if(argv3 || (!argv2))
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=1;
	}
	else if(strcmp(tmp,"register")==0)
	{
		if(!argv4)
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=2;
	}
	else if(strcmp(tmp,"promote")==0)
	{
		if(argv4 || (!argv3))
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=3;
	}
	else if(strcmp(tmp,"find")==0)
	{
		if(argv2 || (!argv1))
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=4;
	}
	else if(strcmp(tmp,"chpasswd")==0)
	{
		if(argv2 || (!argv1))
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=5;
	}
	else if(strcmp(tmp,"delete")==0)
	{
		if(argv2 || (!argv1))
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=6;
	}
	else if(strcmp(tmp,"delself")==0)
	{
		if(argv1)
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=7;
	}
	else if(strcmp(tmp,"logout")==0)
	{
		if(argv1)
		{
			temp->pl_type = 100;
			return;
		}
		temp->pl_type=8;
	}
	else 
		temp->pl_type=100;
	
	if(argv1)
		strcpy(temp->info_t.ctldata.ct_arg1,argv1);
	if(argv2)
		strcpy(temp->info_t.ctldata.ct_arg2,argv2);
	if(argv3)
		strcpy(temp->info_t.ctldata.ct_arg3,argv3);
	if(argv4)
		strcpy(temp->info_t.ctldata.ct_arg4,argv4);



}

int client_download(prol_t * temp,int sockfd)
{
	send(sockfd,temp,sizeof *temp,0);
    int ret = recv(sockfd,temp,sizeof *temp,0);
	if(0 == ret)
	{
		perror("recv in download");
		return -1;
	}

	if(12 != temp->pl_type)
		return 0;


	char buf[100];
	FILE * fd;
	sprintf(buf,"./file/%s",temp->info_t.ctldata.ct_arg1);
	fd = fopen(buf,"w");
	
	while(1)
	{
		ret = recv(sockfd,temp,sizeof *temp,0);
		if(0 == ret)
		{
			perror("recv in download");
			return -1;
		}

		if(!temp->info_t.rdata.tr_flag)
			break;

//	printf("len = %d\n",temp->info_t.rdata.tr_datalen);
//	printf("buf = %s\n",temp->info_t.rdata.data);
	 int ret = fwrite(temp->info_t.rdata.data,1,temp->info_t.rdata.tr_datalen,fd);
//	printf("write = %d\n",ret);
		putchar('#');fflush(stdout);


	}
	fflush(fd);
	return 1;
}

int client_upload(prol_t * temp,int sockfd)
{
	FILE * fd;
	char buf[100];
	sprintf(buf,"./file/%s",temp->info_t.ctldata.ct_arg1);
	fd = fopen(buf,"r");
	if(NULL == fd)
	{
		perror("fopen");
		return -1;
	}
int ret;
	fseek(fd,0,2);
	sprintf(buf,"%ld",ftell(fd));
	strcpy(temp->info_t.ctldata.ct_arg2,buf);
	fseek(fd,0,0);
//	printf("size = %s\n",buf);

	ret = send(sockfd,temp,sizeof *temp,0);

//	printf("148send: %d\n", ret);
    ret = recv(sockfd,temp,sizeof *temp,0);
//	printf("150recv: %d\n", ret);
	if(0 == ret)
	{
		perror("recv in download");
		return -1;
	}
//	printf("temp = %d\n",temp->pl_type);

	if(13 != temp->pl_type)
		return 0;

	temp->pl_type = 0;
	temp->info_t.rdata.tr_flag = 1;
	while(ret = fread(temp->info_t.rdata.data,1,1024,fd))
	{
		temp->info_t.rdata.tr_datalen = ret;
		ret = send(sockfd,temp,sizeof *temp,0);
//	printf("167sendtype = %d\n",temp->pl_type);
//	printf("168send = %d\n",ret);
		putchar('#');fflush(stdout);
	}

//	printf("read = %d\n",ret);
	temp->info_t.rdata.tr_flag = 0;
	ret = send(sockfd,temp,sizeof *temp,0);
//	printf("175sendtype = %d\n",temp->pl_type);
//	printf("176send = %d\n",ret);
	fclose(fd);

	return 1;


}
void print_usage(void)
{
	printf("************************************************\n \
			\r**1,login <username> <passwd>                 **\n \
			\r**2,register <username> <passwd> <age> <phone>**\n \
			\r**3,promote <username> <info> <level>         **\n \
			\r**4,find <username>                           **\n \
			\r**5,chpasswd <newpaswd>                       **\n \
			\r**6,delete <username>                         **\n \
			\r**7,delself                                   **\n \
			\r**8,logout                                    **\n \
			\r************************************************\n \
			\r****note:                                   ****\n \
			\r****  info:passwd,age,phone,salary,flag     ****\n \
			\r****  level:                                ****\n \
			\r****       1:no authority                   ****\n \
			\r****       2:original                       ****\n \
			\r****       3:manager                        ****\n \
			\r************************************************\n");

}
